package com.haoxi.gateway.cmpp30msg;

/**
 * Title:CMPP3.0,2004.09
 */

import java.io.IOException;
import java.io.InputStream;


/**
 * ʵ��CMPP3.0 CMPP_Deliver
 */
public class CMPPDeliver {
    private byte bodybytes[];
    private byte[] Msg_id = new byte[8];
    private String Dest_id;
    private String Service_id;
    private int Tp_pid;
    private int TP_udhi;
    private int Msg_fmt;
    private String Src_terminal_id;
    private int Src_terminal_type;
    private int Registered_delivery;
    private int Msg_Length;
    private byte[] Msg_Content;
    private String LinkID;

    /**
     * ����һ��CMPPDeliver
     */
    public CMPPDeliver () {
    }

    public CMPPDeliver ( byte[] body ) {
        bodybytes = body;
        parseBody ();
    }

    /**
     * ��ȡһ��CMPP_Deliver��
     *
     * @param in ������
     */
    public void read ( InputStream in ) throws IOException {
        CMPPMsgHead head = new CMPPMsgHead ();
        head.read ( in );
        if ( head.getCommandID () == CMPPCommon.CMPP_DELIVER ) {
            bodybytes = new byte[head.getCommandLength () - CMPPCommon.MSG_HEAD_LENGTH];
            in.read ( bodybytes );
        }
        parseBody ();
    }

    private void parseBody () {
        CMPPCommon.BytesCopy ( bodybytes, Msg_id, 0, 7, 0 );
        Dest_id = new String ( bodybytes, 8, 21 ).trim ();
        Service_id = new String ( bodybytes, 29, 10 ).trim ();
        Tp_pid = bodybytes[ 39 ];
        TP_udhi = bodybytes[ 40 ];
        Msg_fmt = bodybytes[ 41 ];
        Src_terminal_id = new String ( bodybytes, 42, 32 ).trim ();
        Src_terminal_type = bodybytes[ 74 ];
        Registered_delivery = bodybytes[ 75 ];
        Msg_Length = bodybytes[ 76 ] & 0xff;
        Msg_Content = new byte[Msg_Length];
        CMPPCommon.BytesCopy ( bodybytes, Msg_Content, 77, 77 + Msg_Length - 1, 0 );
        LinkID = new String ( bodybytes, 77 + Msg_Length, 20 ).trim ();
    }

    /**
     * �μ�CMPP3.0 CMPP_Deliver MsgId
     *
     * @return CMPP_Deliver.MsgId
     */
    public byte[] getMsgId () {
        return Msg_id;
    }

    /**
     * �μ�CMPP3.0 CMPP_Deliver MsgFormat
     *
     * @return CMPP_Deliver.MsgFormat
     */
    public int getMsgFormat () {
        return Msg_fmt;
    }

    /**
     * �μ�Cmpp3.0 CMPP_Deliver Dest_id
     *
     * @return CMPP_Deliver.Dest_id
     */
    public String getDest_id () {
        return Dest_id;
    }

    /**
     * �μ�CMPP3.0 CMPP_Deliver ServiceId
     *
     * @return CMPP_Deliver.ServiceId
     */
    public String getServiceId () {
        return Service_id;
    }

    /**
     * �μ�CMPP3.0 CMPP_Deliver SrcTerminalId
     *
     * @return CMPP_Deliver.SrcTerminalId
     */
    public String getSrcTerminalId () {
        return Src_terminal_id;
    }

    /**
     * �жϴ�Deliver���Ƿ�Ϊ״̬����
     *
     * @return true ��
     *         false ��
     */
    public boolean isStatusRpt () {
        return Registered_delivery != 0;
    }

    /**
     * ȡ��״̬����
     *
     * @return ״̬����
     */
    public CMPPStatusRpt getStatusReport () {
        if ( Registered_delivery != 1 ) {
            return null;
        }
        else {
            CMPPStatusRpt StatRpt = new CMPPStatusRpt ( Msg_Content );
            return StatRpt;
        }
    }

    /**
     * �μ�CMPP3.0 CMPP_Deliver Msglength
     *
     * @return CMPP_Deliver.MsgLength
     */
    public int getMsgLength () {
        return Msg_Length;
    }

    /**
     * �μ�CMPP3.0 CMPP_Deliver Content
     *
     * @return CMPP_Deliver.Msg_Content
     */
    public byte[] getContent () {
        return Msg_Content;
    }

    /**
     * �μ�CMPP3.0 CMPP_Deliver Src_terminal_type
     *
     * @return CMPP_Deliver.Src_terminal_type
     */
    public int getSrc_Terminal_Type () {
        return Src_terminal_type;
    }

    /**
     * �μ�CMPP3.0 CMPP_Deliver LinkID
     *
     * @return CMPP_Deliver.LinkID
     */
    public String getLinkID () {
        return LinkID;
    }

    
}